package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.dao.GoodsMapper;
import com.hl.card.common.persistence.model.Goods;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.GoodsSevice;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/goods")
@Controller
public class GoodsController extends BaseController {

    @Resource
    private GoodsSevice goodsSevice;

    @Resource(name = "goodsMapper")
    private GoodsMapper goodsMapper;

    private String PREFIX = "/packback/goods/";

    @RequestMapping("")
    public String index(){
        return PREFIX+"goods.html";
    }


    @RequestMapping(value = "/goodsadd")
    public Object goodsadd(){
        return PREFIX+"goods_add.html";
    }


    @RequestMapping("/update/{goodsId}")
    public Object update(@PathVariable Integer goodsId, Model model){

        Goods goods = goodsSevice.selectById(goodsId);
        Map<String, Object> goodsMap = BeanKit.beanToMap(goods);
        model.addAttribute("goods",goodsMap);
        LogObjectHolder.me().set(goods);
        return PREFIX+"goods_edit.html";
    }


    @RequestMapping(value = "/insert")
    @ResponseBody
    public Object insert(){
        return PREFIX+"goods_edit.html";
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        goodsSevice.delete(id);
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(){
        List<Map<String, Object>> list= goodsSevice.selectList();
        System.out.println(list);
        return list;
    }
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Goods goods){
        Goods good = goodsMapper.selectByCode(goods.getGoodsCode());
        if(good == null) {
            goodsSevice.save(goods);
            return super.SUCCESS_TIP;
        }else {
            return super.FailuerTip("代码不能重复");

        }

    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(Goods goods){
        goodsSevice.updateById(goods);
        return super.SUCCESS_TIP;
    }

}
